package com.powernode.config;

import cn.hutool.core.util.ObjectUtil;
import com.powernode.constant.AuthConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * feign拦截器  解决服务之间调用没有token的情况
 * @Author: DongGuoZhen
 * @Date: 2025/09/22/15:22
 * @Description:
 */
@Component
public class FeignInterceptor implements RequestInterceptor {



    @Override
    public void apply(RequestTemplate template) {
//        获取当前请求的上下文对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
//        浏览器 -> a -> b
//        判断是否有值   
        if(ObjectUtil.isNotNull(requestAttributes)) {
            HttpServletRequest request = requestAttributes.getRequest();
            if(ObjectUtil.isNotNull(request)) {
//                获取当前请求头中的token值，传递到下一个请求头中
                String authorization = request.getHeader(AuthConstants.AUTHORIZATION);
                template.header(AuthConstants.AUTHORIZATION, authorization);
                return;
            }
        }
// login_token:6ec44e4f-2518-40bc-9116-1f2cfdbada11
        template.header(AuthConstants.AUTHORIZATION,AuthConstants.BEARER + "login_token:6ec44e4f-2518-40bc-9116-1f2cfdbada11");
    }
}
